class AdminController < ApplicationController
  before_filter :authorize
  
  def ship
    count = 0
    if things_to_ship = params[:to_be_shipped]
      count = do_shipping(things_to_ship)
      if count > 0
#        将数字文字化
#        count_text = pluralize(count, "order")
        flash.now[:notice] = "#{count}个订单被处理。"
      end
    end
    @pending_orders = Order.pending_shipping
  end

  private
  def do_shipping(things_to_ship)
    count = 0
    things_to_ship.each do |order_id, do_it|
      if do_it == "yes"
        order = Order.find(order_id)
        order.mark_as_shipped
        order.save
        count += 1
      end
    end
    count
  end
  def pluralize(count, noun)
    case count
    when 0: "No #{noun.pluralize}"
    when 1: "One #{noun}"
    else "#{count} #{noun.pluralize}"
    end
  end
end
